﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.Entity;
using HdipCricketClub.Models;
using HdipCricketClub.ViewModels;
using System.Web.Mvc;

namespace HdipCricketClub.Models
{
    public class ClubFixturePlayerRepository
    {
        CricketClubEntities db = new CricketClubEntities();

        public ClubFixturePlayer GetClubFixturePlayer(int fixtureId, int playerId)
        {
            return db.ClubFixturePlayers.Include("Fixture").Include("Member").Include("DismissalType")
                                        .Where(cfp => cfp.fixture_id == fixtureId && cfp.player_id == playerId)
                                        .Single();
        }

        public List<ClubFixturePlayer> GetClubFixturePlayers(int id)
        {
            return db.ClubFixturePlayers.Include("Fixture").Include("Member").Include("DismissalType").Where(cfp => cfp.fixture_id == id)
                                         .OrderBy(cfp => cfp.Member.last_name).ToList();
        }

        public List<ClubFixturePlayer> GetClubBatsmen(Fixture fixture)
        {
            return db.ClubFixturePlayers.Include("Fixture").Include("Member").Include("DismissalType")
                                        .Where(cfp => cfp.fixture_id == fixture.fixture_id).OrderBy(cfp => cfp.batsman_number)
                                        .OrderBy(cfp => cfp.batsman_number).ToList();
        }

        public List<int> GetClubBatsmenIds(List<ClubFixturePlayer> clubBatsmen)
        {
            List<int> clubBatsmenIds = new List<int>();

            foreach (ClubFixturePlayer batsman in clubBatsmen)
            {
                clubBatsmenIds.Add(batsman.player_id);
            }
            
            return clubBatsmenIds;
        }

        public List<ClubFixturePlayer> GetClubBowlers(Fixture fixture)
        {
            return db.ClubFixturePlayers.Include("Fixture").Include("Member").Include("DismissalType")
                                        .Where(cfp => cfp.fixture_id == fixture.fixture_id && cfp.bowler_number != null)
                                        .OrderBy(cfp => cfp.bowler_number).ToList();
        }

        public double GetTotalBattingRuns(List<ClubFixturePlayer> clubBatsmen)
        {
            double totalRuns = 0;

            foreach (ClubFixturePlayer batsman in clubBatsmen)
            {
                if (batsman.batting_runs != null)
                {
                    totalRuns += (double)batsman.batting_runs;
                }
            }

            return totalRuns;
        }

        public double GetTotalOvers(List<ClubFixturePlayer> players)
        {
            double totalOvers = 0;

            foreach (ClubFixturePlayer player in players)
            {
                if (player.overs != null)
                {
                    totalOvers += (double)player.overs;
                }
            }

            return totalOvers;
        }

        public double GetTotalMaidens(List<ClubFixturePlayer> players)
        {
            double totalMaidens = 0;

            foreach (ClubFixturePlayer player in players)
            {
                if (player.maidens != null)
                {
                    totalMaidens += (double)player.maidens;
                }
            }

            return totalMaidens;
        }

        public double GetTotalBowlingRuns(List<ClubFixturePlayer> players)
        {
            double totalBowlingRuns = 0;

            foreach (ClubFixturePlayer player in players)
            {
                if (player.bowling_runs != null)
                {
                    totalBowlingRuns += (double)player.bowling_runs;
                }
            }

            return totalBowlingRuns;
        }

        public double GetTotalWickets(List<ClubFixturePlayer> players)
        {
            double totalWickets = 0;

            foreach (ClubFixturePlayer player in players)
            {
                if (player.wickets != null)
                {
                    totalWickets += (double)player.wickets;
                }
            }

            return totalWickets;
        }

        public double GetBattingAverage(List<ClubFixturePlayer> players)
        {
            double totalInnings = 0;
            double totalNotOuts = 0;
            double totalRuns = 0;

            foreach (ClubFixturePlayer player in players)
            {
                if ((player.how_out != null) || (player.how_out != 8))
                {
                    totalInnings++;
                }
                if ((player.how_out != null) || (player.how_out == 9))
                {
                    totalNotOuts++;
                }
                if (player.batting_runs != null)
                {
                    totalRuns += (double)player.batting_runs;
                }
            }
            return totalRuns / (totalInnings - totalNotOuts);
        }

        public double GetBowlingAverage(List<ClubFixturePlayer> players)
        {
            double totalRuns = 0;
            double totalWickets = 0;

            foreach (ClubFixturePlayer player in players)
            {
                if (player.bowling_runs != null)
                {
                    totalRuns += (double)player.bowling_runs;
                }
                if (player.bowling_runs != null)
                {
                    totalWickets += (double)player.wickets;
                }
            }
            return totalRuns / totalWickets;
        }

        public List<ClubFixturePlayer> GetAllPlayerFixtures(int playerId)
        {
            return db.ClubFixturePlayers.Where(cfp => cfp.player_id == playerId).OrderBy(cfp => cfp.Fixture.date).ToList();
        }

        public List<ClubFixturePlayer> GetAllPlayerFixturesPlayed(int playerId)
        {
            return db.ClubFixturePlayers.Where(cfp => cfp.player_id == playerId && cfp.how_out != null).OrderBy(cfp => cfp.Fixture.date).ToList();
        }



        public List<ClubFixturePlayer> ParseFormCollection(FormCollection formCollection)
        {
            string[] fixtureId = formCollection.GetValues("item.fixture_id");
            string[] playerId = formCollection.GetValues("item.player_id");
            string[] batsmanNumber = formCollection.GetValues("item.batsman_number");
            string[] bowlerNumber = formCollection.GetValues("item.bowler_number");

            List<ClubFixturePlayer> clubPlayers = new List<ClubFixturePlayer>();

            for(int i = 0; i < playerId.Length; i++)
            {
                int playerFixtureId = Int32.Parse(fixtureId[i]);
                int playerPlayerId = Int32.Parse(playerId[i]);

                ClubFixturePlayer player = GetClubFixturePlayer(playerFixtureId, playerPlayerId);

                if (batsmanNumber[i] != "")
                {
                    player.batsman_number = Int32.Parse(batsmanNumber[i]);
                }
                if (bowlerNumber[i] != "")
                {
                    player.bowler_number = Int32.Parse(bowlerNumber[i]);
                }

                clubPlayers.Add(player);
            }

            List<ClubFixturePlayer> sortedClubPlayers = clubPlayers.OrderBy(cfp => cfp.batsman_number).ToList();

            return sortedClubPlayers;
        }

        public List<ClubFixturePlayer> ParseFormCollectionBattedOnly(FormCollection formCollection)
        {
            string[] fixtureId = formCollection.GetValues("item.fixture_id");
            string[] playerId = formCollection.GetValues("item.player_id");
            string[] batsmanNumber = formCollection.GetValues("item.batsman_number");
            
            List<ClubFixturePlayer> clubPlayers = new List<ClubFixturePlayer>();

            for (int i = 0; i < playerId.Length; i++)
            {
                int playerFixtureId = Int32.Parse(fixtureId[i]);
                int playerPlayerId = Int32.Parse(playerId[i]);

                ClubFixturePlayer player = GetClubFixturePlayer(playerFixtureId, playerPlayerId);

                if (batsmanNumber[i] != "")
                {
                    player.batsman_number = Int32.Parse(batsmanNumber[i]);
                }
                

                clubPlayers.Add(player);
            }

            List<ClubFixturePlayer> sortedClubPlayers = clubPlayers.OrderBy(cfp => cfp.batsman_number).ToList();

            return sortedClubPlayers;
        }

    }
}